<?php

/********************************************************************************* 
 * Power By Oversea , All Right Reserved
 * Class -> used to get the statistic/infomation according to the memeber ID 
 * For oversea member limited
 * ----------------------------------------------
 * Designer: Cavin Zhang
 * Date: Feb 13, 2012
 * ---------------------------------------------
 * Amendment By:
 * Date:
 ******************************************************/

class overseaBasicUser {
	
	protected $usrRealID;
	protected $tempUsrID;
	protected $usrTable;
	protected $fieldsArray= array(); //store the columns which are wanted
	protected $tableLevel = '2'; // 1-> first step table 2->details table
	protected $usrCategory;
	
	public function __construct($temp_userid)
	{
		global $dbObj;
		if(strlen($temp_userid) < 16)
		{
			$this -> tempUsrID = $temp_userid;
			$this -> usrRealID = $this -> getUsrRealId($dbObj);
		}
		else
		{
			$this -> usrRealID = $temp_userid;
			$this -> tempUsrID = $this -> getTempUserId();
		}
		$this->usrCategory = substr($this->usrRealID,2,1);
	}
	
	protected function usrTableName()
	{
		global $tableSelectionArr;
		//$usertype = substr($this->usrRealID,2,1);
		$tableName = $tableSelectionArr[$this->usrCategory.$this->usrCategory];
		return $tableName;
	}
	
	/**
	 * Get the required information from the details table
	 * @param CsDatabaseOpera $db
	 * @param unknown_type $fields
	 */
	public function getUsrInfo(CsDatabaseOpera $db , $fields)
	{
		foreach ($fields as $val)
		{
			$str .= " `".$val."`,"; //consolidate all the colum
		}
		$str = substr($str,0,strlen($str)-1);
		//Added by Liang Fan on 2/3/2012 
		$sql = "SELECT ".$str. " FROM ".$this -> usrTableName()." WHERE `usr_id` = '".mysql_real_escape_string($this->usrRealID)."'"; 
		//errorEventLog::displaysSQLPhase($sql);
		$this->fieldsArray = $db -> getOneQuery($sql);
		return $this->fieldsArray;
	}
	
	
	
	public function getUserEssay(CsDatabaseOpera $db,$limitedNum='')
	{
		//find the total number of the respective catergories
		$tRecorderSet='';
		$tempSum = 0; //used to store the total summary of the essay
		$sql = "SELECT COUNT(*) as total, `chs_name`,`cid` FROM `org_blog` a LEFT JOIN `cs_blog_category` b ON a.blog_classification = b.cid WHERE `usr_id` = '".mysql_real_escape_string($this -> usrRealID)."' GROUP BY `blog_classification`";
		$sql .= ($limitedNum == '')? '':' LIMIT 0,'.$limitedNum;
		$storOffiAnnocmt = $db -> executeQuery($sql);
		if ($storOffiAnnocmt)
		{
			foreach ($storOffiAnnocmt as $val)
			{
				$tempSum += (int)$val['total'];
				$tRecorderSet .= '|'.$val['cid'].'_'.base64_encode($val['chs_name']).'_'.$val['total'];	
			}
		}
		//add the total numbers at the end
		$tRecorderSet = $tempSum.$tRecorderSet;
		//errorEventLog::displaysSQLPhase($tRecorderSet);
		return $tRecorderSet;
	}
	
	public function getUsrRealId(CsDatabaseOpera $db)
	{
		$sql = "SELECT `usr_id` FROM `".$this -> usrTableName()."` WHERE `temp_userid` = '".mysql_real_escape_string($this -> tempUsrID)."'";
		$rs = $db -> getOneQuery($sql);
		if(!$rs){
			header('location:'.WELCOME_COMPASS_HOME.'/'.ERROR_404);
			exit;
		}
		return $rs['usr_id'];
	}
	
	/*
	 * Get the comments posted against to the user's blog
	 * return number is detemined via the 3nd paramater
	 */
	public function getUserEssayComment(CsDatabaseOpera $db, $type,$num = '')
	{
	//modified by Liang Fan on 2/5/2012, for normal usr should redirct to usr's comment table
		if($type == 'share')
		{
			$sql = "SELECT `content`,`blog_id` FROM `cs_kb_comms` WHERE `blog_id` IN (SELECT CONCAT_WS(',',`bid`) FROM `cs_knowledge_base` WHERE `usr_id` = '".mysql_real_escape_string($this -> usrRealID)."') 
					ORDER BY `cmid` DESC LIMIT 0, $num";
		}
		elseif($type == 'ShareAndNews')
		{
			$sql = " (SELECT `content`,`blog_id`,`date_time`, 1 as kb FROM `cs_kb_comms` WHERE `blog_id` IN (SELECT CONCAT_WS(',',`bid`) FROM `cs_knowledge_base` WHERE `usr_id` = '".mysql_real_escape_string($this -> usrRealID)."') )
			UNION ALL(SELECT `content`,`blog_id`,`date_time`, 2 as kb FROM `news_comments` WHERE `blog_id` IN (SELECT CONCAT_WS(',',`id`) FROM `news_info` WHERE `usr_id` = '".mysql_real_escape_string($this -> usrRealID)."') )
					ORDER BY `date_time` DESC LIMIT 0, $num";
		}
		else
		{
			
				$sql = "SELECT `content`,`blog_id` FROM `usr_blog_comments` WHERE `blog_id` IN (SELECT CONCAT_WS(',',`bid`) FROM `usr_blog` WHERE `usr_id` = '".mysql_real_escape_string($this -> usrRealID)."') 
				ORDER BY `cmid` DESC LIMIT 0, $num";
			
		}
		
		$tRecordSet = $db -> executeQuery($sql);
		return $tRecordSet;
	}
	
	
	public function getTempUserId()
	{
		$tempid = (int)substr($this -> usrRealID,8,strlen($this -> usrRealID));
		return 	$tempid;
	}
	
	public static function getUserFriendsList(CsDatabaseOpera $db ,$usrid)
	{
		$tRecordSet = array();
		$sql = "SELECT `fds_id` FROM `usr_relationship` WHERE `usr_id` = '".mysql_real_escape_string($usrid)."'";
		$rs = $db -> executeQuery($sql);
		if($rs)
		{
			foreach ($rs as $val)
			{
				$tRecordSet[] = $val['fds_id'];
			}
		} 
		return $tRecordSet;
	}
	
	public function setUserRealID($userid){
		$this -> usrRealID = $userid;
		$this->usrCategory = substr($userid,2,1);
	}
	
	public function getUserRealID()
	{
		return $this->usrRealID;	
	}
	
	public function getUserCatergory(){
		return $this->usrCategory;
	}

}

?>